package com.ruoze.bdp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoze.bdp.dao.IDataSourceRepository;
import com.ruoze.bdp.dao.ITableOptRepository;
import com.ruoze.bdp.entity.DataSourceEntity;
import com.ruoze.bdp.entity.TableOptEntity;
import com.ruoze.bdp.entity.TableStructVo;
import com.ruoze.bdp.function.DataSourceFunction;
import com.ruoze.bdp.function.DataSourceFunctionBeanFactory;
import com.ruoze.bdp.service.MetaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class MetaServiceImpl implements MetaService {

    @Autowired
    private IDataSourceRepository repository;

    @Autowired
    private DataSourceFunctionBeanFactory factory;

    @Autowired
    private ITableOptRepository tableOptRepository;

    @Override
    public List<String> getMetaDatabases(Long id) {
        DataSourceEntity entity = getDataSourceEntityById(id);
        DataSourceFunction dataSourceFunction = factory.getDataSourceFunction(entity.getType().getDesc());
        return dataSourceFunction.getMetaDatabases(entity.getConnectionParams());
    }

    @Override
    public List<String> getMetaTables(Long id, String database) {
        DataSourceEntity entity = getDataSourceEntityById(id);
        DataSourceFunction dataSourceFunction = factory.getDataSourceFunction(entity.getType().getDesc());
        return dataSourceFunction.getMetaTables(entity.getConnectionParams(), database);
    }

    @Override
    public List<Map<String, String>> getMetaColumns(Long id, String database, String table) {
        DataSourceEntity entity = getDataSourceEntityById(id);
        DataSourceFunction dataSourceFunction = factory.getDataSourceFunction(entity.getType().getDesc());
        return dataSourceFunction.getMetaColumns(entity.getConnectionParams(), database, table);
    }

    @Override
    public boolean createDatabase(Long id, String name) {
        DataSourceEntity entity = getDataSourceEntityById(id);
        DataSourceFunction dataSourceFunction = factory.getDataSourceFunction(entity.getType().getDesc());
        String info = entity.getConnectionParams();
        List<String> metaDatabases = dataSourceFunction.getMetaDatabases(info);
        if(metaDatabases.contains(name)){
            //throw new ServiceException("200010","该数据库已经存在，创建失败");
            return false;
        }
        return dataSourceFunction.createDatabase(info, name);
    }

    @Override
    public boolean createTable(TableStructVo tableVo) {
        DataSourceEntity entity = getDataSourceEntityById(tableVo.getDataSourceId());
        DataSourceFunction dataSourceFunction = factory.getDataSourceFunction(entity.getType().getDesc());
        String info = entity.getConnectionParams();
        List<String> metaTables = dataSourceFunction.getMetaTables(info, tableVo.getDatabaseName());
        if (metaTables.contains(tableVo.getTableName().toLowerCase())) {
            return false;
        }
        boolean succeed = dataSourceFunction.createTable(info, tableVo);
        if (succeed) {
            TableOptEntity tableOptEntity = new TableOptEntity();
            tableOptEntity.setDataSourceId(tableVo.getDataSourceId());
            tableOptEntity.setDatabaseName(tableVo.getDatabaseName());
            tableOptEntity.setTableName(tableVo.getTableName());
            tableOptEntity.setTableOpt(new JSONObject(tableVo.getTableOptions()).toJSONString());
            // todo 补充创建人信息
//            tableOptEntity.setCreatedUserId(tableVo.getCreatedUserId());
//            tableOptEntity.setCreatedUserName(tableVo.getCreatedUserName());
            tableOptRepository.save(tableOptEntity);
        }
        return succeed;
    }

    private DataSourceEntity getDataSourceEntityById(Long id) {
        return repository.findById(id).orElseThrow(() -> new IllegalArgumentException("cannot find relation entity"));
    }
}
